// eslint-disable-next-line import/order
const router = require('koa-router')()
const GenJSONWebToken = require('../utils/genJsonWebToken')
const data = {
  rt_code: 0,
  rt_msg: 'success',
  list: [],
}
for (let index = 0; index < 10; index++) {
  data.list.push({
    typeCode: `dsdssddsd_${index}`,
    typeName: `机械标准零件_${index}`,
    imagePath: 'https://picsum.photos/200/300',
  })
}

router.get('/', async (ctx) => {
  await ctx.render('index', {
    title: 'Hello Koa 2!',
  })
})

// eslint-disable-next-line require-await
router.get('/home/list', async (ctx, next) => {
  ctx.body = data
  next()
})

// eslint-disable-next-line require-await
router.post('/user/login', async (ctx, next) => {
  console.log('dsdsddsd', ctx.body)
  const { username, password } = ctx.request.body
  const token = new GenJSONWebToken({ username, password }).genToken()
  ctx.body = {
    data: {
      code: 0,
      msg: '登录成功',
      token,
    },
  }
  next()
})

module.exports = router
